package marvin.webapp.audio.displaybeans;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import marvin.webapp.audio.util.ConfigReader;
import marvin.webapp.audio.util.MagicTool;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@SessionScoped
public class TreeBean {

	private TreeNode root;
	private FileTreeNode lastSelectedNode;
	private DefaultTreeNode selectedNode;
	
	private List<String> radioURLs;
	
	@PostConstruct
	public void load(){
		try {
			loadNodes();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	public void reloadTree() throws URISyntaxException{
		ConfigReader.readConfiguration();
		loadNodes();
	}
	
	public void loadNodes() throws URISyntaxException {
		List<String> rootFiles = ConfigReader.getParameters(ConfigReader.ROOT_FILES_KEY);
		radioURLs = new ArrayList<String>();
		root = new DefaultTreeNode("/", null);
		
		for(String path : rootFiles){
			String []values = path.split("=");
			String name = values[0];
			String url = "";
			if(values.length > 1){
				for(int i =1; i<values.length; i++)
					url+=values[i];
			}
			
			File rootFile = new File(new URI("file:////"+url));
			FileTreeNode musicNode = new FileTreeNode(name, rootFile, root);

			List<File> childrens;
			if (rootFile.exists() && rootFile.isDirectory()) {
				childrens = MagicTool.getSupportedChildren(rootFile);

				for (File child : childrens) {
					new FileTreeNode(child.getName(), child, musicNode);
				}
			}
		}
		
		/*		
		File rootFile = new File(new URI("file:////MEMUP/Musique/"));
		root = new DefaultTreeNode("/", null);

		FileTreeNode musicNode = new FileTreeNode(rootFile, root);

		List<File> childrens;
		if (rootFile.exists() && rootFile.isDirectory()) {

			childrens = MagicTool.getSupportedChildren(rootFile);

			for (File child : childrens) {
				new FileTreeNode(child, musicNode);
			}
		}
		
		File localRootFile = new File(new URI("file:////D://Local/"));
		FileTreeNode localMusicNode = new FileTreeNode(localRootFile, root);
		File[] localChildrens;
		if (localRootFile.exists() && localRootFile.isDirectory()) {

			localChildrens = localRootFile.listFiles();

			for (File child : localChildrens) {
				new FileTreeNode(child, localMusicNode);
			}
		}*/
		
		
		// Web radios 
		URITreeNode webRoot = new URITreeNode(null, "Web Radios", root);		
		
		List<String>radioValues = ConfigReader.getParameters(ConfigReader.WEB_RADIOS_KEY);
		
		for(String urlValue: radioValues){
			String []values = urlValue.split("=");
			String name = values[0];
			String url = "";
			if(values.length > 1){
				for(int i =1; i<values.length; i++)
					url+=values[i];
			}
			
			radioURLs.add(url);
			new URITreeNode(new URI("http:////"+url), name, webRoot);
		}
		
		
		/*new URITreeNode(new URI("http:////mp3.live.tv-radio.com/rire_et_chansons/all/rir_124629.mp3"), "Rire et Chansons", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/rire_100_blagues/all/rir_123949.mp3"), "Rire et Chansons 100% blagues", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/cherie_fm/all/che_124310.mp3"), "Cherie FM", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/nostalgie_rock/all/nos_145822.mp3"), "Nostalgie Rock", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/nrj_hits/all/nrj_150726.mp3"), "NRJ Hits", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/nrj_pop/all/nrj_151240.mp3"), "NRJ Pop", webRoot);
		new URITreeNode(new URI("http:////mp3.live.tv-radio.com/nrj_rock/all/nrj_161510.mp3"), "NRJ Rock", webRoot);*/
	}
	
	public void onNodeSelect(NodeSelectEvent event) {
		if (event.getTreeNode() instanceof FileTreeNode) {

			FileTreeNode fNode = (FileTreeNode) event.getTreeNode();

			fNode.getChildren().clear();

			List<File> childrens;
			if (fNode.getFile().exists() && fNode.getFile().isDirectory()) {

				childrens = MagicTool.getSupportedChildren(fNode.getFile());
				
				for (File child : childrens) {					
						new FileTreeNode(child.getName(), child, fNode);
				}
			}
			collapseNode(lastSelectedNode);
			expandNode(fNode);
			lastSelectedNode = fNode;
		} else if (event.getTreeNode() instanceof URITreeNode){
			URITreeNode node = (URITreeNode) event.getTreeNode();
			collapseNode(lastSelectedNode);
			expandNode(node);
		}
	}
	
	private void expandNode(TreeNode fNode) {
		if (fNode != null) {
			fNode.setExpanded(true);
			expandNode(fNode.getParent());
		}
	}

	private void collapseNode(TreeNode fNode) {
		if (fNode != null) {
			fNode.setExpanded(false);
			collapseNode(fNode.getParent());
		}
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public FileTreeNode getLastSelectedNode() {
		return lastSelectedNode;
	}

	public void setLastSelectedNode(FileTreeNode lastSelectedNode) {
		this.lastSelectedNode = lastSelectedNode;
	}

	public DefaultTreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(DefaultTreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public List<String> getRadioURLs() {
		return radioURLs;
	}
}
